package com.zq.api.controller;


import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.zq.common.config.Properties.QiNiuYunProperties;
import com.zq.common.core.domain.Result;
import com.zq.common.enums.BusinessEnum;
import com.zq.common.exception.base.BaseException;
import com.zq.server.api.domain.dto.userWxLoginDto;
import com.zq.server.api.service.ApiFileService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


/**
 * 文件上传下载功能
 *
 * @author
 */
@RequestMapping("/file")
@RestController
@Slf4j
@Api(tags = "文件上传下载")
public class ApiFileController {

    @Autowired
    private QiNiuYunProperties qiNiuYunProperties;
    @Autowired
    private ApiFileService fileService;

    /**
     * 上传文件
     *
     * @param imgUrl
     * @return
     */
    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        // 参数校验
        if (file.isEmpty()) {
            return Result.error("文件不能为空");
        }
        // 文件上传
        String fileName = fileService.uploadFile(file);
        // 检查文件上传结果
        if (fileName != null) {
            return Result.success(fileName);
        } else {
            return Result.error("文件上传失败或文件处理错误");
        }
    }


    @PostMapping("file")
    public Result ss(@RequestBody @Validated userWxLoginDto userWxLoginDto) {
        // 这里执行业务逻辑
        throw new BaseException(BusinessEnum.REQUEST_HEADER_FIELDS_TOO_LARGE);
    }
}

